11. SSH

Secure Shell (SSH)
It is a cryptographic network protocol used for securely communicating with another computer.
It allows us to login into a remote machine and execute commands on it.
It uses TCP port 22.
SSH is based on a Unix shell, therefore standard Unix commands can be used to view, modify, and transfer files from a remote machine once an SSH connection has been established.
Uses

How Does SSH Work?

Understanding Different Encryption Techniques

To secure the transmission of information SSH uses three different encryption:

  1. Symmetrical encryption

07e36698535c40f58751dcf7941e8f1b.png

In it a single key is used to encrypt and decrypt the messages.
It's also called “shared secret” encryption, or “secret key” encryption.
SSH use these keys in order to encrypt the entire connection.
Secrety key is generated prior to client authentication.

Key Exchange Algorithm

The client and server both contribute to establish this key through a process known as a key exchange algorithm.
The key is never transmitted between the client and the host. Instead, the two computers share public pieces of data and then manipulate it to independently calculate the secret key.
Before establishing a secured connection both client and server (host) decide which cipher to use like AES, Blowfish, 3DES, CAST128, and Arcfour.

  1. Asymmetrical encryption
    In it two separate keys are used for encryption and decryption.These two keys are known as the public key and the private key. Together they form a public-private key pair.
    The public key can be freely shared with all parties. It is associated with its private key and the private key cannot be derived from the public key.
    A message that is encrypted by a machine’s public key, can only be decrypted by the same machine’s private key.
    Meaning that the public key can not decrypt the messages it encrypt.
    Therefore server and client use each others public keys to encrypt the message and decrypt it with their own private key.

45307bc12baa4c06cdf683878ac6c94b.png

Usage

  1. Hashing
    SSH uses One-way hashing .One-way-hash are not decrypted.
    SSH uses hashes to verify the authenticity of messages. This is done using HMAC (Hash-based Message Authentication Codes). This ensures that the command received is not tampered with in any way.

How it is done?

It is easy to generate a cryptographic hash from a given input, but impossible to reverse it (generate the input from the hash). This means that if a client holds the correct input, they can generate the crypto-graphic hash and compare its value to verify whether they possess the correct input.

Working

52b102b2853da35dec451a5f8aa538f5.png

SSH uses the client-server model.
The host (server) listens on SSH assigned port or 22 for incoming connections.
It organizes the secure connection by authenticating the client and opening the correct shell environment if the verification is successful.

The client begins the initial TCP handshake with the server, negotiating the secure connection, verifying that the server’s identity matches previously recorded information (recorded in an RSA key store file in .ssh directory), and provide credentials for authentication.

An SSH session is established in two stages:

  1. Session Encryption Negotiation

When a client tries to connect to the server (via TCP), the server presents the encryption protocols and respective versions that it supports. If the client has a similar matching pair of protocol and version, an agreement is reached and the connection is started with the accepted protocol.
The server also uses an asymmetric public key which the client can use to verify the authenticity of the host.

Once this is established, the two parties use what is known as a Diffie-Hellman Key Exchange Algorithm to create a symmetrical key. This algorithm allows both the client and the server to arrive at a shared encryption key which will be used henceforth to encrypt the entire communication session.

  1. Authenticating the User

The user is asked to enter the username, followed by the password. These credentials securely pass through the symmetrically encrypted tunnel, so there is no chance of third party sniffing but bots can simply brute force easy or default passwords and gain access.

Therefore, it is recommended to use SSH Key Pairs.These are a set of asymmetric keys used to authenticate the user without the need of inputting any password.

Versions:


SSH In Action

  1. For connection ssh uses symmetric encryption and hash (HMAC) and for authentication ssh uses asymmetric encryption.

  2. Public-Private Key pairs are generated on both client and server independently and not over the network.The public keys from these pairs are shared over the network.
    If key based authentication is used.

Syntax:
To connect to a system:

ssh [USERNAME]@[TARGET IP]

To specify a port, use -p [PORT]

e3a0900c0a3c65806dd570fcec0fdc85.png

Here we are connecting to a Metasploitable2 server (left) using ssh command with username ‘user’ (right).

First the RSA public key of the server is saved in known_hosts file in .ssh directory because we are making connection to the server for the first time.

After that we enter password for ’user’
and execute some Linux commands to get information about user and the server.

Behind the scenes with Wireshark

  1. The client (our machine) begins the initial TCP handshake with the server and sends SSH connection request to the server and the client is running OpenSSH v8.2.

Note: SSH-2 and OpenSSH are essentially the same.

578ed5164d17c83a892ff36f96112187.png

The server is replying to our request using OpenSSH v4.7p1 and now we also know that server is based on Ubuntu or Debian.

da91d714f0ee6c51bfa1548742e9ca60.png

  1. Here, the client tells the server the algorithms it supports for each function (encryption, MAC, key exchange, host authentication, compression) in order of preference.

13fe93111e74afb96eebb51feac679bc.png

The server will send us it’s list.

3238bc4b72744ef8672a65d0e8211208.png

From the two lists of algorithms the same key exchange algorithm will be used.

  1. Now following packets will be shared between both machines:

SSHv2 Client: Diffie-Hellman Key Exchange Init
SSHv2 Server: Diffie-Hellman Key Exchange Reply
SSHv2 Client: Diffie-Hellman GEX Init
SSHv2 Server: Diffie-Hellman GEX Reply

79d3e9388ea99a93d606e43eaecb9c87.png

Above we can see that,

This SSH session will be using AES-128bit in Counter mode for symmetric encryption with an HMAC based upon MD5 to ensure message integrity.

and

Diffie-Hellman Key Exchange Algorithm is used to create the symmetrical key (shared secret).

f2cd6954a7d5e020700b8ed85d606178.png

Note: The reply of the server also contains it’s public host key (identity) or certificates. If the client has never seen it, the client usually asks the user if it should trust it (The RSA key fingerprint in the beginning).

  1. New Keys is an acknowledgment that both will use the same key for communication.

23fd9d28df57c3f7161b1abd309baede.png

  1. All the data sent by client to server will now be encrypted including the user password and commands like whoami,id,uname -a and exit.

19a385d3985233f348cf8c5c383e3f46.png

Below we can see servers public key stored in our machine (in .ssh/known_hosts) and user's public key in server (in .ssh/authorized_keys).

f9a86b0d9c98be0738a3eab5fcaed894.png

SSH can use either "RSA" (Rivest-Shamir-Adleman) or "DSA" (Digital Signature Algorithm) keys.

RSA (Rivest-Shamir-Adleman) is an asymmetric cryptographic algorithm used by modern computers to encrypt and decrypt messages.

DSA is not used anymore as it is less secure.

DSS(Digital Signature Standard) is simply a document that describes the signing procedure and specifies certain standards.

Note: The DSA key present on Metasploitable2 (server) are not generated by our client but we can use it for SSH connection. The key was generated by a client called user and we were able to use it because it is stored in authorised_keys means anyone who know password for user can connect to the server using any machine.